package com.woniuxy.car.commodity.service.core.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.car.commodity.service.core.common.MyAssert;
import com.woniuxy.car.commodity.service.core.dto.CommodityClassDto;
import com.woniuxy.car.commodity.service.core.entity.CommodityClass;
import com.woniuxy.car.commodity.service.core.entity.CommodityFormatTemplate;
import com.woniuxy.car.commodity.service.core.entity.CommodityParamTemplate;
import com.woniuxy.car.commodity.service.core.mapper.CommodityClassMapper;
import com.woniuxy.car.commodity.service.core.mapper.CommodityFormatTemplateMapper;
import com.woniuxy.car.commodity.service.core.mapper.CommodityParamTemplateMapper;
import com.woniuxy.car.commodity.service.core.service.ICommodityClassService;
import com.woniuxy.common.exception.BusinessException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author awz
 * @since 2022-03-01
 */
@AllArgsConstructor
@Service
public class CommodityClassServiceImpl extends ServiceImpl<CommodityClassMapper, CommodityClass> implements ICommodityClassService {

    private CommodityParamTemplateMapper commodityParamTemplateMapper;

    private CommodityFormatTemplateMapper commodityFormatTemplateMapper;

    @Override
    public List<CommodityClass> findCommodityClasses() {

        List<CommodityClass> rootCommodityClasses = baseMapper.selectList(null);
        //最后的结果
        List<CommodityClass> commodityClasses = new ArrayList<>();
        //先找到所有的一级分类
        for (CommodityClass commodityClass : rootCommodityClasses) {
            //一级分类fid为0
            if(commodityClass.getFid() == 0){
                commodityClasses.add(commodityClass);
            }
        }
        //为一级分类设置子分类，getChild是递归调用的
        for (CommodityClass commodityClass : commodityClasses) {
            commodityClass.setChildClasses(getChild(commodityClass.getId(), rootCommodityClasses));
        }

        return commodityClasses;
    }

    @Override
    public void addCommodityClass(CommodityClassDto commodityClassDto) {
        try {
            MyAssert.notEmpty(commodityClassDto.getName(), "类名/品牌名不能为空");
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }
        CommodityClass commodityClass = new CommodityClass();
        //设置父类id
        if(ObjectUtil.isNotEmpty(commodityClassDto.getFid())){
            commodityClass.setFid(commodityClassDto.getFid());
        }
        //设置类别名/品牌名
        commodityClass.setName(commodityClassDto.getName());

        //设置类别图片
        if(ObjectUtil.isNotEmpty(commodityClassDto.getPic())){
            commodityClass.setPic(commodityClassDto.getPic());
        }
        //添加商品类别到数据库
        baseMapper.insert(commodityClass);

        //添加商品参数模板
        if(ObjectUtil.isNotEmpty(commodityClassDto.getCommodityParamTemplate())){
            CommodityParamTemplate commodityParamTemplate = new CommodityParamTemplate();
            commodityParamTemplate.setItemId(commodityClass.getId());
            commodityParamTemplate.setParamData(JSONUtil.parse(commodityClassDto.getCommodityParamTemplate()).toString());
            commodityParamTemplateMapper.insert(commodityParamTemplate);
        }
        //添加商品规格模板
        if(ObjectUtil.isNotEmpty(commodityClassDto.getCommodityFormatTemplate())){
            CommodityFormatTemplate commodityFormatTemplate = new CommodityFormatTemplate();
            commodityFormatTemplate.setItemId(commodityClass.getId());
            commodityFormatTemplate.setFormatData(JSONUtil.parse(commodityClassDto.getCommodityFormatTemplate()).toString());
            commodityFormatTemplateMapper.insert(commodityFormatTemplate);
        }

    }

    private List<CommodityClass> getChild(int id, List<CommodityClass> rootCommodityClasses){
        //子分类
        List<CommodityClass> childList = new ArrayList<>();
        for (CommodityClass rootCommodityClass : rootCommodityClasses) {
            //遍历所有节点，将父类别id与传过来的id比较
            if(rootCommodityClass.getFid() != 0){
                if(rootCommodityClass.getFid() == id){
                    childList.add(rootCommodityClass);
                }
            }
        }
        //把子分类的子分类再循环一遍
        for (CommodityClass commodityClass : childList) {
            commodityClass.setChildClasses(getChild(commodityClass.getId(), rootCommodityClasses));
        }
        if(childList.size() == 0){
            return null;
        }

        return childList;
    }
}
